import 'package:dio/dio.dart';
import 'package:rebase_project/src/base/config/BuildConfig.dart' as config;
import 'ErrorInterceptor.dart';
import 'JsonInterceptor.dart';
import 'LoginStatusInterceptor.dart';
import 'MLog.dart';
import 'SHASignInterceptor.dart';
import 'package:caose/caose.dart' show NetHelp,CacheMixin,BuildConfig;

class CunwNetHelp extends NetHelp with CacheMixin{
  factory CunwNetHelp() => _getInstance();

  static CunwNetHelp get instance => _getInstance();

  static CunwNetHelp _instance;

  static Dio get dio => NetHelp.dio;

  static set dio(value) => NetHelp.dio = value;

  var jsonInterceptor = JsonInterceptor();
  var sha1Interceptor = SHASignInterceptor();
  var loginInterceptor = LoginStatusInterceptor();
  var errorInterceptor = ErrorInterceptor();
  var errorToastInterceptor = ErrorToastInterceptor();
  var logInterceptor = MLogInterceptor();

  CunwNetHelp._internal() {
    dio = Dio()
      ..options.baseUrl = config.CunwBuildConfig.BASE_SERVER
      ..options.connectTimeout = 10000
      ..options.receiveTimeout = 10000;
  }

  static CunwNetHelp _getInstance() {
    if (_instance == null) {
      _instance = CunwNetHelp._internal();
    }
    return _instance..initIntercepter(dio);
  }

  @override
  initIntercepter(Dio dio) {
    dio
      ..interceptors.clear()
      ..interceptors.add(jsonInterceptor)
      ..interceptors.add(sha1Interceptor)
      ..interceptors.add(loginInterceptor)
      ..interceptors.add(errorInterceptor)
      ..interceptors.add(errorToastInterceptor);
    if (!BuildConfig.isRelease) {
      dio.interceptors.add(logInterceptor);
    }
  }

  initDio() {
    var copyDio = Dio(dio.options);
    initIntercepter(copyDio);
    return copyDio;
  }

  Dio clearLoginIntercepter() {
    return initDio()..interceptors.remove(loginInterceptor);
  }

  Dio clearErrorIntercepter() {
    return initDio()..interceptors.remove(errorToastInterceptor);
  }

  Dio clearAllIntercepter() {
    var copyDio = Dio(dio.options);
    if (!BuildConfig.isRelease) {
      copyDio.interceptors..add(logInterceptor)..add(errorInterceptor);
    }
    return copyDio;
  }

  Future<Response> download(urlPath, savePath) async {
    var copyDio = Dio(dio.options);
    return await copyDio.download(urlPath, savePath);
  }

}
// To parse this JSON data, do
//
//     final baseModel = baseModelFromJson(jsonString);
